﻿Option Strict On

Imports Sunts.DataLayer.SQLite
Imports System.Data.SQLite

Public Class BackupJobSchedule
    Public Property BackupScheduleID As Int32
    Public Property BackupJobID As Int32
    Public Property Progress As String
    Public Property ActualStart As DateTime?
    Public Property ActualEnd As DateTime?
    Public Property ScheduleDateTime As DateTime
    Public Property CompletionStatus As String
    Public Property Cancelled As Boolean = False

    Public Function Insert(ConnectionString As String) As Int32
        Try
            Using sdl As New SQLiteDataLayer(ConnectionString)
                sdl.CommandText = "INSERT INTO BackupJobSchedule (BackupJobID, Progress, ActualStart, ActualEnd, ScheduleDateTime) " & _
                                    "VALUES (@BackupJobID, @Progress, @ActualStart, @ActualEnd, @ScheduleDateTime)"

                sdl.ParameterCollection = New SQLiteParamCollection
                sdl.ParameterCollection.Add(New SQLiteParameter("@BackupJobID", Me.BackupJobID))
                sdl.ParameterCollection.Add(New SQLiteParameter("@Progress", Me.Progress))
                sdl.ParameterCollection.Add(New SQLiteParameter("@ActualStart", Me.ActualStart))
                sdl.ParameterCollection.Add(New SQLiteParameter("@ActualEnd", Me.ActualEnd))
                sdl.ParameterCollection.Add(New SQLiteParameter("@ScheduleDateTime", Me.ScheduleDateTime))

                Insert = sdl.ExecuteNonQuery()
            End Using
        Catch ex As Exception
            Throw New Exception("BackupJobSchedule: Insert()" + ControlChars.CrLf + ex.Message + " " + ex.Source + ex.StackTrace)
        End Try

    End Function

    Public Function Update(ConnectionString As String) As Int32
        Try
            Using sdl As New SQLiteDataLayer(ConnectionString)
                sdl.CommandText = "UPDATE BackupJobSchedule SET " & _
                                    "BackupJobID = @BackupJobID, " & _
                                    "Progress = @Progress, " & _
                                    "ActualStart = @ActualStart, " & _
                                    "ActualEnd = @ActualEnd, " & _
                                    "ScheduleDateTime = @ScheduleDateTime, " & _
                                    "CompletionStatus = @CompletionStatus " & _
                                    "WHERE BackupScheduleID = @BackupScheduleID"


                sdl.ParameterCollection = New SQLiteParamCollection
                sdl.ParameterCollection.Add(New SQLiteParameter("@BackupScheduleID", Me.BackupScheduleID))
                sdl.ParameterCollection.Add(New SQLiteParameter("@BackupJobID", Me.BackupJobID))
                sdl.ParameterCollection.Add(New SQLiteParameter("@Progress", Me.Progress))
                sdl.ParameterCollection.Add(New SQLiteParameter("@ActualStart", IIf(IsDBNull(Me.ActualStart), Nothing, Me.ActualStart)))
                sdl.ParameterCollection.Add(New SQLiteParameter("@ActualEnd", IIf(IsDBNull(Me.ActualEnd), Nothing, Me.ActualEnd)))
                sdl.ParameterCollection.Add(New SQLiteParameter("@ScheduleDateTime", Me.ScheduleDateTime))
                sdl.ParameterCollection.Add(New SQLiteParameter("@CompletionStatus", Me.CompletionStatus))

                Update = sdl.ExecuteNonQuery()
            End Using
        Catch ex As Exception
            Throw New Exception("BackupJobSchedule: Update()" + ControlChars.CrLf + ex.Message + " " + ex.Source + ex.StackTrace)
        End Try

    End Function

    Public Shared Function SelectByID(BackupScheduleID As Int32, ConnectionString As String) As BackupJobSchedule
        Dim BackupJobSchedule As BackupJobSchedule
        Dim Reader As SQLiteDataReader

        Try
            BackupJobSchedule = New BackupJobSchedule
            Using sdl As New SQLiteDataLayer(ConnectionString)
                sdl.CommandText = "SELECT * FROM BackupJobSchedule WHERE BackupScheduleID = @BackupScheduleID"

                sdl.ParameterCollection = New SQLiteParamCollection
                sdl.ParameterCollection.Add(New SQLiteParameter("@BackupScheduleID", BackupScheduleID))

                Reader = sdl.ExecuteDataReader

                While Reader.Read
                    BackupJobSchedule = New BackupJobSchedule
                    BackupJobSchedule.BackupScheduleID = CInt(Reader("BackupScheduleID"))
                    BackupJobSchedule.BackupJobID = CInt(Reader("BackupJobID"))
                    BackupJobSchedule.Progress = CStr(IIf(IsDBNull(Reader("Progress")), String.Empty, Reader("Progress")))
                    BackupJobSchedule.ActualStart = CType(IIf(IsDBNull(Reader("ActualStart")), Nothing, Reader("ActualStart")), DateTime?)
                    BackupJobSchedule.ActualEnd = CType(IIf(IsDBNull(Reader("ActualEnd")), Nothing, Reader("ActualEnd")), DateTime?)
                    BackupJobSchedule.ScheduleDateTime = CDate(Reader("ScheduleDateTime"))
                End While
                Reader.Close()
            End Using

            SelectByID = BackupJobSchedule

        Catch ex As Exception
            Throw New Exception("BackupJobSchedule: SelectByID()" + ControlChars.CrLf + ex.Message + " " + ex.Source + ex.StackTrace)
        Finally
            Reader = Nothing
            BackupJobSchedule = Nothing
        End Try
    End Function
End Class